<?php
//管理员实体类
class ManagerModel extends Model {
    public function __construct() {
        parent::__construct();
        //$this->fields = array('id','user','email','passwd','level','login_count','reg_time','last_login_ip','last_login_time');
        $this->tables = array(DB_FREFIX.'manager');
        $this->check = new ManagerCheck();
    }
    /**
    * Add new manager
    *
    * @access public
    * @return string
    */
    public function add() {
        $addData = $this->input->post();
        if(!$this->check->addCheck($this,$addData)) die();
        $addData = $this->input->filter($addData,$this->list_tables());
        $addData['passwd'] = sha1($addData['passwd']);
        $addData['last_login_ip'] = Tool::getIP();
        $addData['reg_time'] = Tool::getDate();
        $addData['last_login_time'] = Tool::getDate();
        return parent::add($addData);
    }
    /**
    * Update one manager
    *
    * @access public
    * @return object
    */
    public function update() {
        $id = $this->input->get('id');
        $where = array("id='$id'");
        if(!$this->check->oneCheck($this,$where)) exit('不存在此数据！');
        $updateData = $this->input->post();
        if(!$this->check->updateCheck($updateData)) die();
        $updateData = $this->input->filter($updateData,$this->list_tables());
        $updateData['passwd'] = $updateData['passwd'] ? sha1($updateData['passwd']) : $this->input->post('password');
        return parent::update($where,$updateData);
    }
    /**
    * Update one manager
    *
    * @access public
    * @return object
    */
    public function ajaxUpdate() {
        $id = $this->input->get('id');
        $where = array("id='$id'");
        $updateData = $this->input->selectAvailableData($this->input->post(),$this->fields);
        return parent::update($where,$updateData);
    }
    /**
    * Check the user when add a manager
    *
    * @access public
    * @return string
    */
    public function isOneByAjax() {
        $user = $this->input->get('user');
        $where = array("user='$user'");
        return parent::isOne($where);
    }
    //
    public function checkLevel() {
        $level = $this->input->get('level');
        $where = array("level='$level'");
        return parent::isOne($where);
    }
    /**
    * Find all manager
    *
    * @access public
    * @return array
    */
    public function findAll() {
        $this->tables = array(DB_FREFIX.'manager AS m',DB_FREFIX.'level AS l');
        return parent::select(array('m.id','m.user','m.level','m.email','m.login_count','m.reg_time','m.last_login_ip','m.last_login_time','l.level_name'),
                       array('where'=>array('m.level=l.id'),
                             'limit'=>$this->limit,
                             'order'=>'m.reg_time DESC'));
    }
    public function findLogin() {
        $this->tables = array(DB_FREFIX.'manager AS m',DB_FREFIX.'level AS l');
        $user = $this->input->post('user');
        $where = array('m.level=l.id',"m.user='$user'");
        return parent::select(array('m.user','l.level_name'),
                              array('where'=>$where,
                                    'limit'=>1));
    }
    /**
    * Update the manager when login
    *
    * @access public
    * @return object
    */
    public function updateLogin() {
        $updateData = array();
        $user = $this->input->post('user');
        $where = array("user='$user'");
        $updateData['login_count'] = array('login_count+1');
        $updateData['last_login_ip'] = Tool::getIP();
        $updateData['last_login_time'] = Tool::getDate();
        return parent::update($where,$updateData);
    }
    /**
    * Delete one manager
    *
    * @access public
    * @return string
    */
    public function del() {
        $id = $this->input->get('id');
        $where = array("id='$id'");
        if(!$this->check->oneCheck($this,$where)) exit('不存在此数据！');
        return parent::del($where);
    }
    /**
     * 取出一条数据
     *
     * @access public
     * @return array
     */
    public function findOne() {
        $id = $this->input->get('id');
        $where = array("id='$id'");
        if(!$this->check->oneCheck($this,$where)) exit('不存在此数据！');
        return parent::select(array('id','user','level','email','passwd'),
                              array('where'=>$where,
                                    'limit'=>'1'));
    }
    /**
     * 总的数据总记录数
     *
     * @access public
     * @return string
     */
    public function total() {
        return parent::total();
    }
    /**
     * 管理员登陆
     *
     * @access public
     * @return array
     */
    public function login() {
        $loginData = $this->input->post();
        if(!$this->check->loginCheck($loginData)) die();
        $user = $this->input->post('user');
        $where = array("user='$user'");
        return parent::login(array('user','passwd'),
                             array('where'=>$where));
    }
}